﻿using System;
using System.ComponentModel;
using CommonClass;

namespace SurveillanceLib
{
    /// <summary>
    /// 文件详情
    /// </summary>
    public class FileDetailModel : INotifyPropertyChanged, IEquatable<FileDetailModel>
    {
        #region Private Fields

        private NetRecordFileInfoStruct _recordFileInfo;

        #endregion

        #region Constructor

        public FileDetailModel()
        {
            _recordFileInfo = new NetRecordFileInfoStruct();
        }

        public FileDetailModel(NetRecordFileInfoStruct recordFileInfo)
        {
            this._recordFileInfo = recordFileInfo;
        }

        #endregion

        #region Public Properties

        #region 与界面绑定的属性

        /// <summary>
        /// 文件是否被占用【播放时 | 下载时 | 转换时】
        /// </summary>
        public bool IsOccupy { get; set; }

        /// <summary>
        /// 所查询的设备
        /// </summary>
        public TagModel SelectedDriver { get; set; }

        /// <summary>
        /// 录像文件信息
        /// </summary>
        public NetRecordFileInfoStruct NetRecordFileInfo
        {
            get
            {
                return this._recordFileInfo;
            }
        }

        private string _driverText;
        /// <summary>
        /// 设备名称
        /// </summary>
        public string DriverText
        {
            get { return _driverText; }
            set
            {
                if (_driverText == value) return;
                _driverText = value;
                RaisePropertyChanged("DriverText");
            }
        }

        private string _driverIP;
        /// <summary>
        /// 设备 IP
        /// </summary>
        public string DriverIP
        {
            get { return _driverIP; }
            set
            {
                if (_driverIP == value) return;
                _driverIP = value;
                RaisePropertyChanged("DriverIP");
            }
        }

        private int _recordFileType;
        /// <summary>
        /// 录像文件夹类型
        /// </summary>
        public int RecordFileType
        {
            get { return _recordFileType; }
            set
            {
                if (_recordFileType == value) return;
                _recordFileType = value;
                RaisePropertyChanged("RecordFileType");
            }
        }

        private string _percentage;
        /// <summary>
        /// 文件操作百分比，为空表示未操作（下载 | 转换）
        /// </summary>
        public string Percentage
        {
            get { return _percentage; }
            set
            {
                if (_percentage == value) return;
                _percentage = value;
                RaisePropertyChanged("Percentage");
            }
        }


        private bool _isSelected;
        /// <summary>
        /// 是否选择
        /// </summary>
        public bool IsSelected
        {
            get { return _isSelected; }
            set
            {
                if (value == _isSelected) return;
                _isSelected = value;
                RaisePropertyChanged("IsSelected");
            }
        }

        #endregion

        /// <summary>
        /// 设备通道号
        /// </summary>
        public uint DriverChannel
        {
            get
            {
                return _recordFileInfo.ch + 1; // 通道号从 0 开始
            }
        }

        /// <summary>
        /// 文件开始时间
        /// </summary>
        public DateTime FileStartTime
        {
            get
            {
                return _recordFileInfo.starttime;
            }
        }

        /// <summary>
        /// 文件结束时间
        /// </summary>
        public DateTime FileEndTime
        {
            get
            {
                return _recordFileInfo.endtime;
            }
        }

        /// <summary>
        /// 文件大小（带单位）
        /// </summary>
        public string FileSize
        {
            get
            {
                return Utilities.ConvertFileSize(_recordFileInfo.size * 1024); // size 单位为 KB
            }
        }

        #endregion

        #region IEquatable<FileDetailModel> 成员

        public bool Equals(FileDetailModel other)
        {
            return this.DriverText == other.DriverText && 
                this.DriverIP == other.DriverIP && 
                this.DriverChannel == other.DriverChannel &&
                this.FileStartTime == other.FileStartTime && 
                this.FileEndTime == other.FileEndTime;
        }

        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion
    }


    /// <summary>
    /// 文件回放
    /// </summary>
    public class FilePlayBackModel : IEquatable<FilePlayBackModel>
    {
        #region Constructor

        public FilePlayBackModel()
        {

        }

        #endregion

        #region Public Properties

        /// <summary>
        /// 文件详情
        /// </summary>
        public FileDetailModel SelectedFile { get; set; }

        /// <summary>
        /// 回放句柄
        /// </summary>
        public string PlayBackHandle { get; set; }

        /// <summary>
        /// 设备 IP
        /// </summary>
        public string DriverIP { get; set; }

        /// <summary>
        /// 设备通道号
        /// </summary>
        public uint DriverChannel { get; set; }

        /// <summary>
        /// 是否播放
        /// </summary>
        public bool IsPlay { get; set; }
        
        #endregion

        #region IEquatable<FilePlayBackModel> 成员

        public bool Equals(FilePlayBackModel other)
        {
            return this.DriverIP == other.DriverIP &&
                this.DriverChannel == other.DriverChannel;
        }

        #endregion
    }
}
